#include "bulletinlist.h"

#include <stdio.h>
#include <QFile>
#include <QTextStream>

bulletinlist::bulletinlist(uint number, QObject *parent) :
    QObject(parent)
{
    m_isOnlyShowTxt = false;

    m_nNumber = number;
    m_scroll = new ScrollLabel(number, (QWidget*)parent);

    Configclass config;
    m_scroll->setScrollSpeed(config.getScrollSpeed());
//    m_scroll->setScrollSpeed(10);


    m_scroll->setWordColor(238, 235, 73);
    m_scroll->setFontWidth(30);
    m_scroll->setBGColor(0, 0, 255);

    m_isPlayFinish =true;
    m_playTimer = new QTimer(this);
    m_playTimer->setSingleShot(true);
    connect(m_playTimer, SIGNAL(timeout()), this, SLOT(timeToStop()));



}


bulletinlist::~bulletinlist()
{
    m_playTimer->stop();
    delete m_playTimer;

    if(m_scroll != NULL)
    {
        m_scroll->stop();
        delete m_scroll;
    }
}

void bulletinlist::initList()
{
    m_fileList.clear();
    m_playTimeList.clear();
}

void bulletinlist::updateList(const RoutePkgStruct& routePkg)
{
    m_isOnlyShowTxt = false;
    m_scroll->setOnlyShowTxt(m_isOnlyShowTxt);

    if(m_scroll == NULL)
    {
        return;
    }

    m_scroll->setRect(QRect(routePkg.layoutStruct.x,
                                routePkg.layoutStruct.y,
                                routePkg.layoutStruct.width,
                                routePkg.layoutStruct.height));
    m_scroll->setPartitionID(routePkg.layoutStruct.partitionID);


    for(int i = 0;i < routePkg.contentStructList.count(); i++)
    {
        m_fileList.append(routePkg.contentStructList.at(i).contentFile);
        m_playTimeList.append(routePkg.contentStructList.at(i).duration);
    }
    m_nCur = 0;
    m_isPlayFinish = false;

//    QFile file(routePkg.contentStructList.at(0).contentFile);

    m_playTimer->start(m_playTimeList.at(m_nCur) * 1000);
    if (!loadTextFile(routePkg.contentStructList.at(0).contentFile))      //暂时只实现一个文件的处理
    {
        m_nCur++;
        if(m_nCur >= m_fileList.count())
        {
            emit msgArrive(m_nNumber, 3);
            m_nCur = 0;
            m_isPlayFinish = true;
        }
        this->loadTextFile(m_fileList.at(m_nCur));
    }
}


bool bulletinlist::isStop()
{
    return m_isPlayFinish;
}

void bulletinlist::setScrollSpeed(const int speed)
{
    m_scroll->setScrollSpeed(speed);
}

void bulletinlist::stop()
{
    m_scroll->stop();
}

void bulletinlist::hide()
{
//    this->stop();
    m_scroll->hide();
}

void bulletinlist::show()
{
//    m_scroll->start();
    m_scroll->show();
}

void bulletinlist::updateDateState(const QString &msg)
{
//    qDebug() << "msgmsg===>" << msg;
    m_scroll->setGeometry(0, 0, GLOBAL_RESOLUTION_WIDTH, GLOBAL_RESOLUTION_HEIGHT);
    m_scroll->repaint();
    m_scroll->setRect(0, 0, GLOBAL_RESOLUTION_WIDTH, GLOBAL_RESOLUTION_HEIGHT);
//    m_scroll->setScrollText(msg);
    m_scroll->setScrollSpeed(90);
    m_scroll->setOnlyShowTxt(true, msg);

//    m_scroll->start();
}

void bulletinlist::onlyShowText(const RoutePkgStruct &routePkg)
{
    QString filePath;

    if(m_scroll == NULL)
    {
        return;
    }
    m_scroll->setRect(QRect(routePkg.layoutStruct.x,
                                routePkg.layoutStruct.y,
                                routePkg.layoutStruct.width,
                                routePkg.layoutStruct.height));
    m_scroll->setPartitionID(routePkg.layoutStruct.partitionID);

    m_fileList.clear();
    for(int i = 0;i < routePkg.contentStructList.count(); i++)
    {
        m_fileList.append(routePkg.contentStructList.at(i).contentFile);
        m_playTimeList.append(routePkg.contentStructList.at(i).duration);
    }
    m_nCur = 0;
    m_isPlayFinish = false;

    filePath = m_fileList.at(0);

    m_isOnlyShowTxt = true;
    m_scroll->setOnlyShowTxt(m_isOnlyShowTxt);

    QString fullPath(g_installPath);
    fullPath.append("/");
    fullPath.append(filePath);

    //ivan @2011.10.09 add
    fullPath.replace("\\", "/");

    QString text;
    QFile file1(fullPath);
    if (file1.open(QFile::ReadOnly))
    {

        QTextStream in(&file1);
        in.setCodec("UTF-8"); //请注意这行

        while(!in.atEnd())
        {
            QString line = in.readAll();
            text.append(line);
        }
    } else {
        logger()->warn("Txtfile \"%1\" open fail\n", fullPath);
        return ;
    }
    m_scroll->setOnlyShowTxt(true, text);

    return;
}

bool bulletinlist::loadTextFile(const QString& filePath)
{
//    qDebug() << "================================ <<<< loadTextFile >>>> =============================";

    QString fullPath(g_installPath);
    fullPath.append("/");
    fullPath.append(filePath);


    //ivan @2011.10.09 add
    fullPath.replace("\\", "/");

    QString text;
    QFile file(fullPath);
    if (file.open(QFile::ReadOnly))
    {

        QTextStream in(&file);
        in.setCodec("UTF-8"); //请注意这行

        while(!in.atEnd())
        {
            QString line = in.readLine();
            text.append(line);
            text.append("               ");
        }
    } else {   
        logger()->warn("TxtFile \"%1\" open fail\n", fullPath);
        return false;
    }
//    qDebug() << "text------------------------------>" << text;
////////////////////////////////////
   // m_scroll->setText(QString::fromLocal8Bit("welcome to metro HK line one，For safety, please stand clear of the door. Thank you!                                                                      "));
    m_scroll->setScrollText(text);
   // m_scroll->setLogoPath("./logo.PNG");
//    m_scroll->show();
    m_scroll->start();

    return true;
}

void bulletinlist::timeToStop()
{

    m_scroll->stop();
    m_nCur++;
    if(m_nCur >= m_fileList.count())
    {
        emit msgArrive(m_nNumber, 3);
        m_nCur = 0;
        m_isPlayFinish = true;
    }
    m_playTimer->start(m_playTimeList.at(m_nCur) * 1000);
    if (!loadTextFile(m_fileList.at(m_nCur)))      //暂时只实现一个文件的处理
    {
        m_nCur++;
        if(m_nCur >= m_fileList.count())
        {
            emit msgArrive(m_nNumber, 3);
            m_nCur = 0;
            m_isPlayFinish = true;
        }
        this->loadTextFile(m_fileList.at(m_nCur));
    }
}
